﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OnlineStore.Bll;
using OnlineStore.Dal;

namespace OnlineStore.Gui
{
    public partial class Edit : System.Web.UI.Page
    {
        protected string picture {get;set;}
        EditControl editcontrol = new EditControl();
        protected void Page_Load(object sender, EventArgs e)
        {
            bizProduct _biz = new bizProduct();
            tbProduct tbProduct = _biz.SearchPorduct(Request.QueryString.Get("id"));

            txtName.Text = tbProduct.Product_Name.ToString();
            txtIdFather.Text = tbProduct.Father.ToString();
            ddlType.SelectedValue=tbProduct.Type;
            ddlSize.SelectedValue= tbProduct.Size;
            txtColor.Text = tbProduct.Color.ToString();
            txtIntroduce.InnerText = tbProduct.Introduce.ToString();
            txtDetail.InnerText = tbProduct.Detail.ToString();
            txtPurQty.Text = tbProduct.Pur_Qty.ToString();
            txtOrdQty.Text = tbProduct.Ord_Qty.ToString();
            txtStoQty.Text = tbProduct.Stock_Qty.ToString();
            txtPrice.Text= tbProduct.Price.ToString();
            picture=tbProduct.Picture.ToString();
            
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (editcontrol.isFather(txtIdFather.Text) == false)
            {
                lblFather.Text = "Bạn đã nhập sai id father";
            }
            else
            {
                if (txtName.Text.ToString() == string.Empty || txtIdFather.Text == string.Empty || txtColor.Text == string.Empty || txtIntroduce.InnerText == string.Empty || txtDetail.InnerText.ToString() == string.Empty || txtPrice.Text == string.Empty)
                {
                    lblWarn.Text = "Bạn đã nhập thiếu";
                    lblWarn.Visible = true;
                }
                else
                {
                    tbProduct tbProduct = new tbProduct();
                    tbProduct.Product_Name = txtName.Text.ToString();
                    tbProduct.Father = Int32.Parse(txtIdFather.Text.ToString());
                    tbProduct.Type = ddlType.SelectedValue.ToString();
                    tbProduct.Size = ddlSize.SelectedValue.ToString();
                    tbProduct.Color = txtColor.Text.ToString();
                    tbProduct.Introduce = txtIntroduce.InnerText.ToString();
                    tbProduct.Detail = txtDetail.InnerText.ToString();
                    tbProduct.Pur_Qty = int.Parse("0" + txtPurQty.Text.ToString());
                    tbProduct.Ord_Qty = int.Parse("0" + txtOrdQty.Text.ToString());
                    tbProduct.Stock_Qty = int.Parse("0" + txtStoQty.Text.ToString());
                    tbProduct.Price = double.Parse("0" + txtPrice.Text.ToString());
                    if (fupHinh.FileName.ToString() == string.Empty)
                    {
                        tbProduct.Picture = picture;
                    }
                    else
                    {
                        tbProduct.Picture = fupHinh.FileName.ToString();
                        //xu ly dua hinh vao csdl;
                    }

                    
                    if (editcontrol.EditProduct(tbProduct.Product_Name, (int)tbProduct.Father, tbProduct.Type, tbProduct.Size, tbProduct.Color, tbProduct.Introduce, tbProduct.Detail, (int)tbProduct.Pur_Qty, (int)tbProduct.Pur_Qty, (int)tbProduct.Stock_Qty, (double)tbProduct.Price, tbProduct.Picture))
                    {
                    }
                    else
                    {
                    }
                }
            }
                
        }

        protected void btnEditCancel_Click(object sender, EventArgs e)
        {
            bizProduct _biz = new bizProduct();
            tbProduct tbProduct = _biz.SearchPorduct(Request.QueryString.Get("id"));

            txtName.Text = tbProduct.Product_Name.ToString();
            txtIdFather.Text = tbProduct.Father.ToString();
            ddlType.SelectedValue = tbProduct.Type;
            ddlSize.SelectedValue = tbProduct.Size;
            txtColor.Text = tbProduct.Color.ToString();
            txtIntroduce.InnerText = tbProduct.Introduce.ToString();
            txtDetail.InnerText = tbProduct.Detail.ToString();
            txtPurQty.Text = tbProduct.Pur_Qty.ToString();
            txtOrdQty.Text = tbProduct.Ord_Qty.ToString();
            txtStoQty.Text = tbProduct.Stock_Qty.ToString();
            txtPrice.Text = tbProduct.Price.ToString();
            picture = tbProduct.Picture.ToString();
        }

        protected void btnCheckIdFather_Click(object sender, EventArgs e)
        {
            if (addcontrol.isFather(txtIdFather.Text) == false)
            {
                lblFather.Text = "Bạn đã nhập sai id father";
            }
            else
            {
                //thuc hien kiem intro+detail
                //hien intro+detail
                //txtIntroduce.InnerText=
                //txtDetail.InnerText=
            }
        }
    }
}